import { defineConfig } from "vite";
import * as path from "path";

const { resolve } = path;

export default defineConfig(({ command, mode, ssrBuild }) => {
  const common = {
    // 项目根目录，index.html 所在的目录
    // 要配置多页面，所以此处更改项目根目录地址，不再是项目根目录
    // 而是指定的目录下， 以便配置多页面index.html入口
    root: resolve(__dirname, "src/pages"),
    resolve: {
      // 设置文件目录别名
      // 根目录地址变更，也需要调整
      alias: {
        "@": resolve(__dirname, "./src"),
      },
    },
  };

  if (command === "serve") {
    return {
      // dev 独有配置
      build: {
        rollupOptions: {
          input: {
            home: resolve("src/pages/home/home.html"),
            about: resolve("src/pages/about/about.html"),
          },
        },
      },
      ...common,
    };
  } else {
    // command === 'build'
    return {
      // build 独有配置

      // 静态资源服务目录地址
      // 根目录变化，原来的public静态资源目录则需要，指向
      publicDir: resolve(__dirname, "./public"),
      // 存储缓存文件的目录地址
      cacheDir: "",
      build: {
        outDir: resolve(__dirname, "dist"),
        sourcemap: true,
        rollupOptions: {
          input: {
            home: resolve("src/pages/home/home.html"),
            about: resolve("src/pages/about/about.html"),
          },
          output: {},
        },
      },
    };
  }
});
